﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Rtgs.master" AutoEventWireup="true"
    CodeFile="change_passwordpage.aspx.cs" Inherits="page_usermanagement_change_passwordpage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="body" runat="Server">
    <link href="<%= ResolveClientUrl("~/css/validationEngine.jquery.css") %>" rel="stylesheet"
        type="text/css" />
    <script src="<%= ResolveClientUrl("~/script/jquery.validationEngine.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveClientUrl("~/script/languages/jquery.validationEngine-en.js") %>"
        type="text/javascript"></script>
    <script src="<%= ResolveClientUrl("~/script/helper.js") %>" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#<%=txtUserName.ClientID %>').val('');
            $('#<%=txtNewPassword.ClientID %>').val('');
            $('#<%=txtVerifyPassword.ClientID %>').val('');

            // binds form submission and fields to the validation engine
            jQuery("#form1").validationEngine();

            //            jQuery("#form1").submit();
            if ($("#form1").validationEngine('validate')) { }
            else { jQuery("#form1").submit(); }

            populateData();

            $("#save").click(function () {

                var password = $('#<%= txtNewPassword.ClientID %>').val();
                var verifypassword = $('#<%= txtVerifyPassword.ClientID %>').val();
                var username = $('#<%= txtUserName.ClientID %>').val();

                if (username == "") {
                    alert("Kolom User Name harus diisi..!!");
                    return;
                }

                if (password == "") {
                    alert("Kolom New Password harus diisi..!!");
                    return;
                }

                if (verifypassword == "") {
                    alert("Kolom New Password harus diisi..!!");
                    return;
                }

                if ($("#form1").validationEngine('validate')) {
                    var userId = $('#<%= hfUserID.ClientID %>').val();
                    if (verifypassword == password) {

                        $.ajax({
                            type: 'POST',
                            url: '../../ajaxhandler/usermanagement/sys_t_user.ashx?method=getsamepassword&id=' + userId+'&password='+password,
                            contentType: 'application/json',
                            dataType: 'json',
                            success: function (data) {
                                if (data.samepassword == "true") {
                                    alert("Password baru tidak boleh sama dengan password lama..!!");

                                    $('#<%= txtNewPassword.ClientID %>').val('');
                                    $('#<%= txtVerifyPassword.ClientID %>').val('');

                                    $('#<%= txtNewPassword.ClientID %>').focus();
                                    
                                }
                                else {

                                    var handlerUrl = '<%= ResolveClientUrl("~/ajaxhandler/usermanagement/sys_t_user.ashx") %>';
                                    var id = $('#<%= hfUserID.ClientID %>').val();
                                    var password = $('#<%= txtNewPassword.ClientID %>').val();
                                    var param = '&userid=' + id + '&password=' + password;
                                    CallAjax(handlerUrl, "?method=changepassword" + param);
                                }
                            },
                            error: function () {
                                alert('Error!');
                            }
                        });

                    }
                    else {

                        alert("New Password dan Verify Password harus sama ..!!");
                        $('#<%= txtNewPassword.ClientID %>').val('');
                        $('#<%= txtVerifyPassword.ClientID %>').val('');
                        $('#<%= txtNewPassword.ClientID %>').focus();
                    }

                }

                return false;
            });

        });

        function populateData() {
            var availableTags = [];

            var urlPath = '<%= ResolveClientUrl("~/ajaxhandler/usermanagement/sys_t_user.ashx") %>';
            $.ajax({
                type: 'POST',
                url: urlPath + '?method=getautocomplate',
                contentType: 'application/json',
                dataType: 'json',
                success: function (datas) {
                    jQuery.each(datas, function (i, data) {
                        availableTags.push(data);
                    });

                    $('#<%=txtUserName.ClientID %>').autocomplete({
                        source: availableTags,
                        select: function (event, ui) {
                            $('#<%=txtUserName.ClientID %>').val(ui.item.label);
                            $('#<%=hfUserID.ClientID %>').val(ui.item.value);
                            $('#<%=txtNewPassword.ClientID %>').val('');
                            $('#<%=txtVerifyPassword.ClientID %>').val('');
                            return false;
                        }

                    });

                },
                error: function () {
                    alert('Error!');
                }
            });
        }
    </script>
    <div>
        <table border="0" cellpadding="0" cellspacing="0">
            <tr>
                <td>
                    User Name
                </td>
                <td style="padding-left: 30px">
                    :
                </td>
                <td style="padding-left: 30px">
                    <asp:TextBox ID="txtUserName" runat="server" CssClass="validate[required]" Width="200px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    New Password
                </td>
                <td style="padding-left: 30px">
                    :
                </td>
                <td style="padding-left: 30px">
                    <asp:TextBox ID="txtNewPassword" TextMode="Password" CssClass="validate[required]" runat="server" MaxLength="50"
                        Width="200"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Verify Password
                </td>
                <td style="padding-left: 30px">
                    :
                </td>
                <td style="padding-left: 30px">
                    <asp:TextBox ID="txtVerifyPassword" TextMode="Password" MaxLength="50" CssClass="validate[required]"
                        runat="server" Width="200"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    &nbsp;
                </td>
                <td>
                    <div style="text-align: right; padding-top: 10px;">
                        <a href="#" id="save" class="dialog_link ui-state-default ui-corner-all" alt="Save">
                            <span class="ui-icon ui-icon-disk"></span>Save</a>
                    </div>
                </td>
            </tr>
        </table>
        <asp:HiddenField ID="hfUserID" runat="server" />
    </div>
</asp:Content>
